<?php

include_once 'Super_Model.php';

class Application_Model_CatsAndTags extends Super_Model {

    public function getCategories() {
        $sql = 'SELECT `url` FROM `categories` Where `url` != \'\'';
        $cats = $this->GetAll($sql);
        $result = array();
        if (is_array($cats) && count($cats)) {
            foreach ($cats as $cat) {
                $result[] = $cat['url'];
            }
            return $result;
        }
        return $result;
    }

    public function getChildCatsies($parent_id) {
        $sql = 'SELECT `id` FROM `categories` Where `parent_id` = '.(int)$parent_id.' AND `url` != \'\'';
        $cats = $this->GetAll($sql);
        $result = array();
        if (is_array($cats) && count($cats)) {
            foreach ($cats as $cat) {
                $result[] = $cat['id'];
            }
            return $result;
        }
        return $result;
    }

    public function getTags() {
        $sql = 'SELECT `url` FROM `labels` Where `url` != \'\'';
        $tags = $this->GetAll($sql);
        $result = array();
        if (is_array($tags) && count($tags)) {
            foreach ($tags as $cat) {
                $result[] = $cat['url'];
            }
            return $result;
        }
        return $result;
    }

    public function getCategoryByUrl($url) {
        $sql = 'SELECT * FROM `categories` Where `url` = \'' . addslashes($url) . '\'';
        $cat_id = $this->getRow($sql);
        if ($cat_id) {
            return $cat_id;
        }
        return false;
    }

    public function getCategoryByID($cat_id) {
        $sql = 'SELECT * FROM `categories` Where `id` = ' . intval($cat_id);
        $cat_id = $this->getRow($sql);
        if ($cat_id) {
            return $cat_id;
        }
        return false;
    }

    public function getTagsByUrl($url) {

        $sql = 'SELECT * FROM `labels` Where `url` = \'' . addslashes($url) . '\'';
        $cat_id = $this->getRow($sql);
        if ($cat_id) {
            return $cat_id;
        }
        return false;
    }

}